<template>
  <div class="role-container">
    <div class="left-wrapper">
      <!--
        思路： tab切换类交互的实现 通用思路
        1. 点击谁就会把谁的 index 值记录下来
        2. 通过动态控制 class 类名 active   :class="{ active:  index === 当前项的下标值 }"
      -->
      <div
        v-for="(item,index) in roleList"
        :key="item.roleId"
        class="role-item"
        @click="switchTab(index)"
        :class="{ active:  index === activeIndex }"
      >
        <div class="role-info">
          <!--
            svg-icon 是一个组件, 而且是一个 全局组件
            1.传入不同的iconClass 就会实现不同的图标效果
            2. 图标名来自于icons/svg 文件夹下的所有可选图标的名称
            目标:  根据当前谁被激活 显示激活的图标
            未激活  user  激活的图标叫做 user-active
          -->

          <svg-icon :icon-class="activeIndex === index ? 'user-active' : 'user'" class="icon"/>
          {{ item.roleName }}
        </div>
        <div class="more">
          <el-dropdown>
            <span class="el-dropdown-link">
          <svg-icon icon-class="more"/>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="handleEdit(item.roleId)">编辑角色</el-dropdown-item>
              <el-dropdown-item @click.native="handleDelete(item.roleId)">删除角色</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
      <el-button class="addBtn" size="mini" @click="handleAdd">添加角色</el-button>
    </div>

    <!--    =============  右侧    ============  -->
    <div class="right-wrapper">
      <el-tabs v-model="activeName">
        <el-tab-pane label="功能权限" name="permission">
          <div class="tree-wrapper">
            <div v-for="item in treeList" :key="item.id" class="tree-item">
              <div class="tree-title"> {{ item.title }}</div>
              <el-tree
                :props="{ label: 'title', children: 'children' }"
                show-checkbox
                default-expand-all
                ref="nodeTree"
                node-key="id"
                :data="item.children"
              >
              </el-tree>
            </div>
          </div>
        </el-tab-pane>
        <el-tab-pane label="成员列表" name="user">
          <div class="user-wrapper">
            <el-table :data="roleUserList" style="width: 100%">
              <el-table-column type="index" label="序号" width="250"/>
              <el-table-column prop="name" label="员工姓名"/>
              <el-table-column prop="userName" label="登录账号"/>
            </el-table>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
import { getRoleListAPI, getTreeListAPI, getRolePermisionAPI, getRoleUserAPI, delRoleUserAPI } from '@/api/role'

// 递归添加 disabled 属性 为true
function addDisabled(data) {
  data.forEach(item => {
    item.disabled = true
    if (item.children) {
      addDisabled(item.children)
    }
  })
}

export default {
  name: 'Role',
  data() {
    return {
      roleList: [],
      activeIndex: 0,
      activeName: 'permission',
      params: {
        pageNum: 1,
        pageSize: 10
      },
      treeList: [],
      roleUserList: [],
      total: 0
    }
  },
  async mounted() {
    await this.getRoleList()
    await this.getTreeList()
    // 组件初始化的时候 模拟点击一下  第一项
    this.switchTab(0)
  },
  methods: {
    async getRoleList() {
      // 调用接口获取角色列表
      const res = await getRoleListAPI(this.params)
      if (res.code === 10000) {
        this.roleList = res.data
      }
    },
    async getTreeList() {
      // 调用接口获取角色列表
      const res = await getTreeListAPI()
      if (res.code === 10000) {
        // 直接赋值 没有disabled 为true
        // 思路: 针对于 treeList 中的每一项(每一层中的每一项) 都动态添加 一个 disabled 属性 为true
        this.treeList = res.data
        addDisabled(this.treeList)
        // console.log(this.treeList)
      }
    },
    async switchTab(index) {
      this.activeIndex = index
      // this.getTreeList()
      // 1.拿到当前的角色id
      const roleId = this.roleList[index].roleId
      // 2. 调用接口 获取当前角色的权限树
      const res = await getRolePermisionAPI(roleId)
      if (res.code === 10000) {
        const rolePermisionList = res.data.perms
        // console.log('========= rolePermisionList =========', rolePermisionList)
        // 2.1 遍历  找到当前角色的权限树
        this.$refs.nodeTree.forEach((node, index) => {
          // console.log('========= node =========', node)
          node.setCheckedKeys(rolePermisionList[index])
        })
      }
      // 实现成员列表  接口的调用
      const result = await getRoleUserAPI(roleId)
      if (result.code === 10000) {
        console.log('========= result =========', result)
        this.roleUserList = result.data.rows
        this.total = result.data.total
      }
    },
    handleClick(index) {
      this.activeIndex = index
      this.activeName = 'first'
    },
    handleTabClick(tab, event) {
      this.activeName = tab.name
    },
    handleAdd() {
      this.$router.push({ path: '/addRole' })
    },
    handleEdit(id) {
      // console.log('handleEdit', id)
      this.$router.push({
        path: '/addRole',
        query: {
          roleId: id
        }
      })
    },
    handleDelete(id) {
      console.log('handleDelete', id)
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 调用接口 删除角色
        const result = await delRoleUserAPI(id)
        // 1. 先删除角色的权限树
        if (result.code === 10000) {
          await this.getRoleList()
          this.$message.success('删除成功')
        }
      }).catch(() => {
        console.log('取消删除')
      })
    }
  }
}
</script>

<style scoped lang="scss">
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;
  padding: 20px;

  .left-wrapper {
    width: 200px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;

      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
      }
    }

    .more {
      display: flex;
      align-items: center;
    }

    .addBtn {
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;

    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;

        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }

    ::v-deep .el-tabs__nav-wrap {
      padding-left: 20px;
    }

    .user-wrapper {
      padding: 20px;
    }
  }
}
</style>
